﻿/* ----------------------------------------------------------------------------
 * Copyright (c) Philip Stears 
 *
 * This source code is subject to terms and conditions of the Microsoft Public 
 * License (Ms-PL).
 * 
 * By using this source code in any fashion, you are agreeing to be bound 
 * by the terms of the Microsoft Public License.
 * 
 * To request a copy of the Microsoft Public License, send a request by email
 * to license@connectbasic.net.
 *
 * This notice must not be removed from this software.
 * ---------------------------------------------------------------------------- */

namespace ConnectBasic.ObjectModel
{
    using System.Collections.Generic;
    using ConnectBasic.Execution;
    using DlrExpression = Microsoft.Linq.Expressions.Expression;

    /// <summary>
    /// Represents a while statement.
    /// </summary>
    public class WhileStatement : StatementNodeBase
    {
        private ExpressionBase mCondition;
        private Block mStatements;

        /// <summary>
        /// Initializes a new instance of the <see cref="WhileStatement" /> class.
        /// </summary>
        /// <param name="condition">The condition which gets evaluated with each iteration of the loop.</param>
        /// <param name="statements">The statements which get executed with each iteration of the loop.</param>
        public WhileStatement(ExpressionBase condition, IEnumerable<StatementNodeBase> statements)
            : this(condition, new Block(statements))
        {
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="WhileStatement" /> class.
        /// </summary>
        /// <param name="condition">The condition which gets evaluated with each iteration of the loop.</param>
        /// <param name="statements">The statements which get executed with each iteration of the loop.</param>
        public WhileStatement(ExpressionBase condition, Block statements)
        {
            mCondition = condition;
            mStatements = statements;
        }

        /// <summary>
        /// Gets the condition which gets evaluated with each iteration of the loop.
        /// </summary>
        public ExpressionBase Condition
        {
            get { return mCondition; }
        }

        /// <summary>
        /// Gets the statements which get executed with each iteration of the loop.
        /// </summary>
        public Block Statements
        {
            get { return mStatements; }
        }

        /// <inheritdoc />
        public override Microsoft.Linq.Expressions.Expression Compile(ConnectBasic.Execution.BasicCodeGenerator gen)
        {

            // Make sure we're setup in case one of the statements is an Exit While
            var exitLabel = DlrExpression.Label();

            gen.PushFlowContext(BasicTokenKind.KeywordWhile, exitLabel);
            var whileStatements = mStatements.Compile(gen);
            gen.PopFlowContext();

            var whileExpression = Microsoft.Scripting.Ast.Utils.While(
                                    mCondition.Compile(gen),
                                    whileStatements,
                                    null,
                                    exitLabel, 
                                    null
                                  ).Reduce();

            return whileExpression;
        }
    }
}
